package com.bicycle.rentalsystem.servelt;



import com.bicycle.rentalsystem.service.UserService;
import com.bicycle.rentalsystem.service.impl.user.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * servlet用于处理前端发送的请求
 * 从前端获取数据并将其传往service层
 *
 * 该例用于处理login请求
 * 目前不需要更改
 */
@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        int result = userService.login(username,password);
        if (result == 1){
            req.getSession().setAttribute("username",username);
            Integer identity = userService.getIdentity(username);
            if (identity == 1){
                result = 3;
            }
        }
        PrintWriter writer = resp.getWriter();
        writer.print(result);
        writer.flush();
        writer.close();
    }
}
